package com.softstao.guoyu.ui.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.softstao.guoyu.R;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.Address;
import com.softstao.guoyu.model.me.AddressList;
import com.softstao.guoyu.mvp.interactor.me.AddressInteractor;
import com.softstao.guoyu.mvp.presenter.me.AddressPresenter;
import com.softstao.guoyu.mvp.viewer.me.AddressListViewer;
import com.softstao.guoyu.ui.activity.BaseListActivity;
import com.softstao.guoyu.ui.adapter.AddressAdapter;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.softstao.softstaolibrary.library.widget.MarginDecoration2;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

/**
 * Created by vaikus on 2017/6/22.
 */

public class AddressListActivity extends BaseListActivity<Address> implements AddressListViewer,AddressAdapter.AddressListener{

    @AIPresenter(presenter = AddressPresenter.class,interactor = AddressInteractor.class)
    AddressPresenter presenter;

    private int position;
    private boolean isCart=false;

    @Override
    public void initView() {
        super.initView();
        isCart = getIntent().getBooleanExtra("cart",false);
        initTitle("管理收货地址");
        titleBar.setRightIcon(R.mipmap.title_btn_plus);
        titleBar.setRightIconOnClick(v -> {
            Intent intent = new Intent(context,AddAddressActivity.class);
            intent.putExtra("flag",1);
            startActivity(intent);
        });

        adapter = new AddressAdapter(datas);
        ((AddressAdapter)adapter).setAddressListener(this);
        adapter.setListener(position1 -> {
            if(isCart){
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("address",datas.get(position1));
                intent.putExtras(bundle);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        mRecycleView.setAdapter(adapter);
        mRecycleView.setLayoutManager(new FullyLinearLayoutManager(this));
        mRecycleView.addItemDecoration(new MarginDecoration2(this));
    }

    @Override
    public void edit(int position) {
        this.position = position;
        Intent intent = new Intent(getContext(),AddAddressActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("address",datas.get(position));
        bundle.putInt("flag",0);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void delete(int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setPositiveButton("确定",(dialog, which) -> {
            this.position = position;
            deleteAddress();
            dialog.dismiss();
        }).setNegativeButton("取消",(dialog, which) ->
                dialog.dismiss()
        ).setMessage("确定删除此收货地址？").create().show();
    }

    @Override
    public void isDefault(int position) {
        this.position = position;
        changeDefault();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getAddress();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        getAddress();
    }

    @Override
    public void getAddress() {
        loading.setVisibility(View.VISIBLE);
        presenter.getAddress(SharePreferenceManager.getInstance().getAgentId());
    }

    @Override
    public void changeDefault() {
        loading.setVisibility(View.VISIBLE);
        presenter.changeDefault(SharePreferenceManager.getInstance().getAgentId(),datas.get(position).getAddressId());
    }

    @Override
    public void deleteAddress() {
        presenter.deleteAddress(SharePreferenceManager.getInstance().getAgentId(),datas.get(position).getAddressId());
    }

    @Override
    public void getList(AddressList addressList) {
        loading.setVisibility(View.GONE);
        if (addressList!=null){
            if (addressList.getAddressList()!=null&&addressList.getAddressList().size()!=0){
                emptyLayout.setVisibility(View.GONE);
                datas.clear();
                datas.addAll(addressList.getAddressList());
                adapter.notifyDataSetChanged();
            }else {
                isEmpty();
            }
        }
    }

    @Override
    public void changeResult(AddressList addressList) {
        loading.setVisibility(View.GONE);
        datas.get(((AddressAdapter)adapter).getPreSelected()).setDefaultX(false);
        datas.get(position).setDefaultX(true);
        adapter.notifyItemChanged(((AddressAdapter)adapter).getPreSelected());
        adapter.notifyItemChanged(position);
        ((AddressAdapter)adapter).setPreSelected(position);
    }

    @Override
    public void deleteResult(Object result) {
        datas.remove(position);
        adapter.notifyItemRemoved(position);
        LZToast.getInstance(context).showToast("删除成功");
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
